package com.mcc.app.productionMat.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.common.utils.DateUtils;
import com.mcc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.app.productionMat.mapper.RawMaterialCostAnalysisMapper;
import com.mcc.app.productionMat.domain.RawMaterialCostAnalysis;
import com.mcc.app.productionMat.service.IRawMaterialCostAnalysisService;

/**
 * 原料成本分析Service业务层处理
 *
 * @author gxy
 * @date 2025-03-21
 */
@Service
public class RawMaterialCostAnalysisServiceImpl implements IRawMaterialCostAnalysisService {

    @Autowired
    private RawMaterialCostAnalysisMapper rawMaterialCostAnalysisMapper;

    /**
     * 查询原料成本分析
     *
     * @param id 原料成本分析ID
     * @return 原料成本分析
     */
    @Override
    public RawMaterialCostAnalysis selectRawMaterialCostAnalysisById(Long id) {
        return rawMaterialCostAnalysisMapper.selectById(id);
    }

    /**
     * 查询原料成本分析列表
     *
     * @param rawMaterialCostAnalysis 原料成本分析
     * @return 原料成本分析
     */
    @Override
    public List<RawMaterialCostAnalysis> selectRawMaterialCostAnalysisList(RawMaterialCostAnalysis rawMaterialCostAnalysis) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(rawMaterialCostAnalysis)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getRawDate()), "raw_date", rawMaterialCostAnalysis.getRawDate());
            queryWrapper.like(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getClassesName()), "classes_name", rawMaterialCostAnalysis.getClassesName());
            queryWrapper.like(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getTeamName()), "team_name", rawMaterialCostAnalysis.getTeamName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getDateType()), "date_type", rawMaterialCostAnalysis.getDateType());
            queryWrapper.eq(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getConsumptionTotal()), "consumption_total", rawMaterialCostAnalysis.getConsumptionTotal());
            queryWrapper.eq(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getConsumptionCost()), "consumption_cost", rawMaterialCostAnalysis.getConsumptionCost());
        }
        queryWrapper.orderByDesc("raw_date");
        return rawMaterialCostAnalysisMapper.selectList(queryWrapper);
    }

    /**
     * 新增原料成本分析
     *
     * @param rawMaterialCostAnalysis 原料成本分析
     * @return 结果
     */
    @Override
    public int insertRawMaterialCostAnalysis(RawMaterialCostAnalysis rawMaterialCostAnalysis) {
        rawMaterialCostAnalysis.setCreateTime(new Date());
//        rawMaterialCostAnalysis.setCreateBy(SecurityUtils.getUsername());
        return rawMaterialCostAnalysisMapper.insert(rawMaterialCostAnalysis);
    }

    /**
     * 修改原料成本分析
     *
     * @param rawMaterialCostAnalysis 原料成本分析
     * @return 结果
     */
    @Override
    public int updateRawMaterialCostAnalysis(RawMaterialCostAnalysis rawMaterialCostAnalysis) {
        rawMaterialCostAnalysis.setUpdateTime(new Date());
//        rawMaterialCostAnalysis.setUpdateBy(SecurityUtils.getUsername());
        return rawMaterialCostAnalysisMapper.updateById(rawMaterialCostAnalysis);
    }

    /**
     * 删除原料成本分析
     *
     * @param id 原料成本分析ID
     * @return 结果
     */
    @Override
    public int deleteRawMaterialCostAnalysisById(Long id) {
        return rawMaterialCostAnalysisMapper.deleteById(id);
    }

    /**
     * 批量删除原料成本分析
     *
     * @param ids 原料成本分析ID
     * @return 结果
     */
    @Override
    public int deleteRawMaterialCostAnalysisByIds(Long[] ids) {
        if (ids.length == 0) return 0;
        return rawMaterialCostAnalysisMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    @Override
    public RawMaterialCostAnalysis selectRawMaterialCostAnalysisByCond(RawMaterialCostAnalysis rawMaterialCostAnalysis){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(rawMaterialCostAnalysis)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getRawDate()), "raw_date", rawMaterialCostAnalysis.getRawDate());
            queryWrapper.eq(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getClassesName()), "classes_name", rawMaterialCostAnalysis.getClassesName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getTeamName()), "team_name", rawMaterialCostAnalysis.getTeamName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(rawMaterialCostAnalysis.getDateType()), "date_type", rawMaterialCostAnalysis.getDateType());
        }
        queryWrapper.last("limit 1");
        return rawMaterialCostAnalysisMapper.selectOne(queryWrapper);
    }
}
